package com.yutel.phone.binh;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;

import com.yutel.phone.binh.service.BinhService;
import com.yutel.phone.binh.service.ServiceReceiver;
import com.yutel.phone.binh.service.SyncService;
import com.yutel.phone.binh.ui.BaseActivity;
import com.yutel.phone.binh.ui.HomeActivity;

public class BinhActivity extends BaseActivity implements ServiceReceiver.Receiver {
	final static String TAG = "BinhActivity";
	private ServiceReceiver mReceiver;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mReceiver = new ServiceReceiver(new Handler());
		mReceiver.setReceiver(this);
		checkNetworkInfo();
	}
	@Override
	protected void onRestart() {
		super.onRestart();
		init();
	}
	private void checkNetworkInfo() {
		ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo info = conMan.getActiveNetworkInfo();
		Log.i(TAG, "info=" + info);
		if (info == null) {
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle(R.string.dialog_title);
			builder.setIcon(android.R.drawable.ic_dialog_alert);
			builder.setMessage(R.string.net_disnable);
			builder.setPositiveButton(R.string.btn_confirm,
					new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							startActivity(new Intent(
									Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
							dialog.dismiss();
						}
					});
			builder.setNegativeButton(R.string.btn_cancel,
					new OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							init();
						}
					});
			builder.create().show();
		} else {
			init();
		}
	}
	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case SyncService.STATUS_RUNNING:
			break;
		case SyncService.STATUS_FINISHED:
			// updateView(resultData);
			goHome();
			break;
		case SyncService.STATUS_ERROR:
			// resultCount.setText(R.string.network_timeout);
			// footerView.setVisibility(View.GONE);
			// searchBtn.setEnabled(true);
			goHome();
			break;
		default:
			Log.w(TAG, "warring resultCode=" + resultCode);
			goHome();
		}
	}
	private void init() {
		Intent intent = new Intent(Intent.ACTION_SYNC, null, this,
				BinhService.class);
		intent.putExtra(SyncService.EXTRA_STATUS_RECEIVER, mReceiver);
		startService(intent);
	}
	private void goHome() {
		Intent intent = new Intent(this, HomeActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		finish();
	}
}